package com.fm.service;

import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
import java.util.Scanner;

import static com.fm.service.service.TCPClient;

public class service_read {

    public static void read(String num) {
        File file = new File("src/main/resources/DownLoad");
        File[] files = file.listFiles();
        int i=1;
        for (File file1 : files){
            System.out.println((i++)+". "+file1.getName());
        }

        // 从键盘获取阅读书本
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要阅读的书本序号: ");
        String SerialNumber = scanner.nextLine();
        try {
            int len;
            i=1;
            FileInputStream fileInputStream = null;
            for (File file1 : files){
                if((i++) == Integer.parseInt(SerialNumber)){
                    fileInputStream = new FileInputStream("src/main/resources/Download/"+file1.getName());
                    byte[] bytes = new byte[1024];
                    while ((len=(fileInputStream.read(bytes)))!=-1){
                        System.out.print(new String(bytes,0,len));
                    }
                    fileInputStream.close();
                    break;
                }

            }


        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
